package com.zjj.springmvc.dao.impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import com.zjj.springmvc.dao.UserDao;
import com.zjj.springmvc.entity.User;

public class UserDaoImpl implements UserDao {

	
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public List<User> queryUserList() {
		String sql = "select user_id,user_name from t_userinfo where user_id=?";
		List<User> list = jdbcTemplate.query(sql, new RowMapper<User>() {
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {
				User user = new User();
				user.setId(rs.getString("user_id"));
				user.setName(rs.getString("user_name"));
				return user;
			}
		}, 63);
		return list;
	}
	
	/**
	 * 
	 * @param paramMap
	 * @return query(String sql, RowMapper<Map<String, Object>> rowMapper, Object... args)
	 */
	public List<Map<String, Object>> queryUserList(Map<String, String> paramMap) {
		String sql = "select user_id,user_name from t_userinfo where user_id=?";
		List<Map<String, Object>> list = jdbcTemplate.query(sql, new RowMapper<Map<String, Object>>() {
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				ResultSetMetaData md = rs.getMetaData();
				int columnCount = md.getColumnCount();
				Map<String, Object> rowData = new HashMap<String, Object>();
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));
				}
				return rowData;
			}
		}, 63);
		return list;
	}
	
	@Override
	public List<Map<String, Object>> queryObjectUserList(Map<String, String> paramMap) {
		String status = paramMap.get("status");
		String stuId = paramMap.get("stuId");

		List<String> paramList = new ArrayList<String>();
		StringBuilder sql = new StringBuilder("select u.user_id,u.user_name,d.datebook_id,d.date_time,b.book_name");
		sql.append(" from t_datebook as d,t_book as b,t_userinfo as u");
		sql.append(" where u.user_id = d.stu_id and d.book_id = b.book_id");
		if (StringUtils.isNotBlank(status)) {
			sql.append(" AND d.status = ?");
			paramList.add(status);
		}
		if (StringUtils.isNotBlank(stuId)) {
			sql.append(" AND u.user_id = ?");
			paramList.add(stuId);
		}
		sql.append("LIMIT 10");
		Object[] obj = paramList.toArray();
		return jdbcTemplate.query(sql.toString(), obj, new DbResultRowMapper());
	}
	
	@Transactional
	public boolean updateUserInfo() {
		Object[] obj = new Object[] { "test333", "2018-03-14 15:49:09", "70" };
		String updateSql = "UPDATE t_userinfo SET user_name = ?,update_time=? WHERE user_id = ?";
		int resultCode = jdbcTemplate.update(updateSql, obj);
		// name唯一性索引
		String insertSql = "insert into t_test2(name) values(?)"; 
		int resultCode2 = jdbcTemplate.update(insertSql, "hellow9");
		return resultCode >= 1 && resultCode2 >= 1;
	}

	@Override
	public Map<String, Object> queryProduct() {
		return null;
	}
	
	
}
